package com.test.ml.yqs.service;


import com.test.ml.yqs.entity.CookieVariable;
import com.test.ml.yqs.entity.SystemVariable;
import com.test.ml.yqs.utills.HttpClientUtil;
import com.test.ml.yqs.utills.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 重庆时时彩
 * --下注
 * @author 彭长云
 *
 */
@Service
public class CqsscBetService {
	@Autowired
	private LoginService loginService;

	@Value("${cqssc.autotou.url}")
	private    String  AUTOTOU_URL;
	@Value("${cqssc.seckey.url}")
	private    String  SECKEY_URL;

	/**
	 * 下注
	 * @param betcode 下注编号
	 * @param betMoney
	 */
	public boolean postbet(String betcode,String betMoney){
		//如果未登陆，调用登陆方法
		if(CookieVariable.win_session==null){
			loginService.login("xiaomage123qwe", "qwe123");
		}
		
			Map<String, String> seckeyMap=new HashMap<String, String>();
			seckeyMap.put("game_code", "2");
			seckeyMap.put("type", "0");
			
			//获取最新的key
			String seckey=null;
			try {
			 seckey= JsonUtils.getString(HttpClientUtil.doPost(seckeyMap, SECKEY_URL).getJsonResult(),"newkey");
			} catch (Exception e) {
				e.printStackTrace();
				loginService.login("xiaomage123qwe", "qwe123");
//				 seckey=JsonUtils.getString(HttpClientUtil.doPost(seckeyMap, SECKEY_URL).getJsonResult(),"newkey");
				 return false;
			}
			Map<String, String> paramterMap=new HashMap<String, String>();
			paramterMap.put(betcode, betMoney);//key:球号大小单双等，value投注金额;"newip_5-1006"--五球小
			paramterMap.put("seckey",seckey);
			paramterMap.put("game_code", "2");//游戏编码，2表示重庆时时彩
			paramterMap.put("typecode", "0");
			paramterMap.put("round", SystemVariable.pid);//投注的期数
			HttpClientUtil.doPost(paramterMap, AUTOTOU_URL);
			return true;
		
	}


	/**
	 *
	 * @param betmap 下注号和金额的map
	 * @return
	 */
	public boolean postbetMap(Map<String,Integer> betmap){

		//如果未登陆，调用登陆方法
		if(CookieVariable.win_session==null){
			loginService.login("xiaomage123qwe", "qwe123");
		}

		Map<String, String> seckeyMap=new HashMap<String, String>();
		seckeyMap.put("game_code", "2");
		seckeyMap.put("type", "0");

		//获取最新的key
		String seckey=null;
		try {
			seckey=JsonUtils.getString(HttpClientUtil.doPost(seckeyMap, SECKEY_URL).getJsonResult(),"newkey");
		} catch (Exception e) {
			e.printStackTrace();
			loginService.login("xiaomage123qwe", "qwe123");
//				 seckey=JsonUtils.getString(HttpClientUtil.doPost(seckeyMap, SECKEY_URL).getJsonResult(),"newkey");
			return false;
		}
		Map<String, String> paramterMap=new HashMap<String, String>();
//		paramterMap.putAll(betmap);
		for(String key :betmap.keySet()){
			paramterMap.put(key,betmap.get(key).toString());
		}
		paramterMap.put("seckey",seckey);
		paramterMap.put("game_code", "2");//游戏编码，2表示重庆时时彩
		paramterMap.put("typecode", "0");
		paramterMap.put("round", SystemVariable.pid);//投注的期数
		HttpClientUtil.doPost(paramterMap, AUTOTOU_URL);
		return true;

	}
}
